/**
 * 
 */
package com.serial.java;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author LionBule
 * 2011-2-11 ����10:21:37
 */
public class ObjectSerializeUtil {
	
	public static byte[] serialize(Object obj) throws IOException {
		if(obj==null)
			throw new NullPointerException();
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream    out = new ObjectOutputStream(bos);
		
		out.writeObject(obj);
		out.flush();
		out.close();
		
		byte[] _data = null;
		_data = bos.toByteArray();
		bos = null;
		
		return _data;
	}
	
	public static Object deserialize(byte[] by) throws Exception {
		if(by==null) 
			throw new NullPointerException();
		
		Object _data = null;
		InputStream is = new ByteArrayInputStream(by);
		ObjectInputStream in = new ObjectInputStream(is);
		_data = in.readObject();
		in.close();
		in = null;
		is = null;
		
		return _data;
	}	
}
